<template>
  <div class="app-container">
    <!--工具条-->
    <e-header :query="query"/>

    <!--列表-->
    <el-table :data="teachers" border highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="60"/>
      <el-table-column prop="name" label="姓名" min-width="100"/>
      <el-table-column prop="mobile" label="联系电话" min-width="180"/>
      <el-table-column prop="gender" label="性别" min-width="100" :formatter="formatgender"/>
      <el-table-column prop="birth" label="生日" width="120" :formatter="dateFormatter"/>
      <el-table-column prop="status" label="账号状态" width="160"/>
      <el-table-column prop="hiredata" label="入职时间" min-width="120" :formatter="entryFormatter"/>
      <el-table-column label="操作" width="160">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页组件-->
    <div class="pg-wrapper">
      <el-pagination
        :total="total"
        layout="total, prev, pager, next, sizes, jumper"
        @size-change="sizeChange"
        @current-change="pageChange"/>
    </div>
    
    <!--新增界面-->
    <el-dialog title="新增教师" :visible.sync="addFormVisible" :close-on-click-modal="false" @open="look()">
      <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="addForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="addForm.gender" size="medium">
            <el-radio border label="男"></el-radio>
            <el-radio border label="女"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="介绍">
          <el-input v-model="addForm.intro"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import initData from '@/mixins/initData'
import eHeader from './module/header'
// import util from '../../common/js/util'
//import NProgress from 'nprogress'
import {
  getTeacherList,
  addTeacher
} from '@/api/teacher';

export default {
  data() {
    return {
      filters: {
        name: ''
      },
      teachers: [],
      total: 0,
      page: 1,
      listLoading: false,
      sels: [], //列表选中列

      addFormVisible: false, //新增界面是否显示
      addLoading: false,
      addFormRules: {
        name: [{
          required: true,
          message: '请输入教师名称',
          trigger: 'blur'
        }]
      },
      //新增界面数据
      addForm: {
        name: '',
        gender: -1
      }
    }
  },
  filters: {
    formatDate(time) {
      var date = new Date(time)
      return formatDate(date, 'yyyy-MM-dd hh:mm')
    }
  },
  methods: {
    look() {
      console.log("opening the dialog.");
    },
    dateFormatter: function (row, column) {
      // return util.formatDate.format(new Date(row.birth), 'yyyy-MM-dd');
    },
    entryFormatter: function (row, column) {
      // return util.formatDate.format(new Date(row.hiredate), 'yyyy-MM-dd');
    },
    // 性别显示转换
    formatgender: function (row, column) {
      return row.gender == 1 ? '男' : row.gender == 0 ? '女' : '未知'
    },
    handleCurrentChange(val) {
      this.page = val
      this.getStudents()
    },
    getTeachers() {
      let para = {
        page: this.page,
        size: 20,
        name: this.filters.name
      };
      this.listLoading = true
      getTeacherList(para).then(res => {
        this.total = res.size
        this.teachers = res.list
        this.listLoading = false
      });
    },
    selsChange: function (sels) {
      this.sels = sels
    },
    // 显示新增界面
    handleAdd: function () {
      this.addFormVisible = true;
      this.addForm = {
        name: '',
        gender: -1,
        avatar: 0,
        intro: ''
      }
    },
    // 显示编辑界面
    handleEdit: function (index, row) {
      this.editFormVisible = true;
      // this.editForm = Object.assign({}, row);
    },
    // 新增
    addSubmit: function () {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.addLoading = false
            // NProgress.start();
            let para = Object.assign({}, this.addForm)
            var _this = this
            addTeacher(para).then(res => {
              _this.addLoading = false
              // NProgress.done()
              this.$message({
                message: "提交成功",
                type: "success"
              })
              this.$refs["addForm"].resetFields()
              this.addFormVisible = false
              this.getTeachers();
            })
          });
        }
      });
    },
  },
  mounted() {
    this.getTeachers()
  }
}
</script>
